package com.controller;

import com.exception.NotFoundException;
import com.model.Details;
import com.model.Tutorial;
import com.repository.DetailsRepository;
import com.repository.TutorialRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Optional;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/api")
public class DetailController {

    @Autowired
    DetailsRepository detailsRepository;

    @Autowired
    TutorialRepository tutorialRepository;

    //根据ID查找detail
    @GetMapping({"/detail/{id}","/tutorials/{id}/details"})
    public ResponseEntity<Details> getDetailById(@PathVariable("id")Long id){
//        Optional<Details> details = detailsRepository.findById(id);
//        System.out.println(details);
//        if(details.isPresent()){
//            return new ResponseEntity<>(details.get(), HttpStatus.OK);
//        }else{
//            return new ResponseEntity<>(null,HttpStatus.NOT_FOUND);
//        }

        Details details = detailsRepository.findById(id)
                .orElseThrow(() ->new NotFoundException("没有找到id:"+id));
        Details details1 = detailsRepository.findById(id).get();
        System.out.println(details1);
        System.out.println(details);
        return new ResponseEntity<>(details,HttpStatus.OK);
    }

    //新增details
    @PostMapping("/tutorials/{id}/details")
    public ResponseEntity<Details> addDetail(@PathVariable("id")Long id,@RequestBody Details details){

        Optional<Tutorial> tutorial = tutorialRepository.findById(id);
        if(tutorial.isPresent()){
            details.setCreatedOn(new Date());
            details.setTutorial(tutorial.get());
            Details details1 = detailsRepository.save(details);
            return new ResponseEntity<>(details1,HttpStatus.CREATED);
        }else{
            return new ResponseEntity<>(null,HttpStatus.NOT_FOUND);
        }

//        Tutorial tutorial = tutorialRepository.findById(id)
//                .orElseThrow(() -> new NotFoundException("没有找到id："+id));
//
//        details.setCreatedOn(new Date());
//        details.setTutorial(tutorial);
//
//        return new ResponseEntity<>(detailsRepository.save(details),HttpStatus.CREATED);

    }

    //根据id修改details
    @PutMapping("/details/{id}")
    public ResponseEntity<Details> updateDetails(@PathVariable("id")Long id,@RequestBody Details details){
        Optional<Details> details1 = detailsRepository.findById(id);
        if(details1.isPresent()){
            Details details2 = details1.get();
            details2.setCreatedBy(details.getCreatedBy());
            return new ResponseEntity<>(detailsRepository.save(details2),HttpStatus.OK);
        }else{
            return new ResponseEntity<>(null,HttpStatus.NOT_FOUND);
        }
    }

    //根据id删除details
    @DeleteMapping("/details/{id}")
    public ResponseEntity<Details> deleteById(@PathVariable("id")Long id){
        detailsRepository.deleteById(id);

        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    //根据tutorialId删除details
    @DeleteMapping("/tutorials/{id}/details")
    public ResponseEntity<Details> deleteByTutorialId(@PathVariable("id")Long id){
        if(tutorialRepository.existsById(id)){
            detailsRepository.deleteByTutorialId(id);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }else{
            throw new NotFoundException("没有找到id："+id);
        }

    }


    //删除tutorialsAndDetails
    @DeleteMapping("/tutorialsAndDetails/{id}")
    public ResponseEntity<Details> deleteTutorialAndDetail(@PathVariable("id")Long id){
        detailsRepository.deleteByTutorialId(id);
        tutorialRepository.deleteById(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


}
